package com.hjj.springboot.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.properties.WeixinProperties;
import com.hjj.springboot.service.ICommentService;
import com.hjj.springboot.service.IProductService;
import com.hjj.springboot.service.IWxUserInfoService;
import com.hjj.springboot.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IWxUserInfoService wxUserInfoService;

    @Autowired
    private IProductService productService;

    @Autowired
    private WeixinProperties weixinProperties;

    @Autowired
    private HttpClientUtil httpClientUtil;

    //根据商品id查看评论信息
    @GetMapping("/goodCommentList")
    public R goodCommentList(Integer gId){
        List<Comment> commentList = commentService.list(new QueryWrapper<Comment>().eq("gId", gId).orderByDesc("upTime"));
        for (Comment comment : commentList) {
            WxUserInfo wxUserInfo = wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openId", comment.getUId()));
            comment.setWxUserInfo(wxUserInfo);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message",commentList);
        return R.ok(map);
    }


    //根据用户openid查看评论信息
    @GetMapping ("/userCommentList")
    public R userCommentList(String openid){

        List<Comment> commentList = commentService.list(new QueryWrapper<Comment>().eq("uId", openid).orderByDesc("upTime"));
        for (Comment comment : commentList) {
           Product product= productService.getOne(new QueryWrapper<Product>().eq("id", comment.getGId()));
           comment.setProduct(product);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message",commentList);
        return R.ok(map);
    }

    @GetMapping("/userDelete")
    public R userDelete(Integer id){
        commentService.removeById(id);
        return R.ok();
    }

    @PostMapping("/create")
    public R create(@RequestBody List<Comment> commList){
        for (Comment comment : commList) {
            comment.setUpTime(new Date());
        }
        System.out.println(commList);
        commentService.saveBatch(commList);
        return R.ok();
    }

    @GetMapping("/scoreList")
    public R scoreList(){
        List<Comment> commentList = commentService.list();
        // 将评论按商品ID分组
        Map<Integer, List<Comment>> groupedComments = commentList.stream()
                .collect(Collectors.groupingBy(Comment::getGId));
        // 计算每个商品的平均评分
        List<Map<String, Object>> averageScores = groupedComments.entrySet().stream()
                .map(entry -> {
                    Integer productId = entry.getKey();
                    Product product = productService.getOne(new QueryWrapper<Product>().eq("id", productId));
                    double averageScore = entry.getValue().stream()
                            .mapToInt(Comment::getScores)
                            .average()
                            .orElse(0.0); // 如果没有评分，则默认为0.0
                    Map<String, Object> productScore = new HashMap<>();
                    productScore.put("product", product);
                    productScore.put("averageScore", averageScore);
                    return productScore;
                })
                // 对averageScores按照平均评分从高到低排序
                .sorted((map1, map2) -> Double.compare((Double) map2.get("averageScore"), (Double) map1.get("averageScore")))
                // 只取前8条数据
                .limit(8)
                .collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("message", averageScores);
        return R.ok(map);
    }

}
